package cn.com.bluemoon.daps.standard.service.impl;

import cn.com.bluemoon.daps.standard.entity.DapStandardBasic;
import cn.com.bluemoon.daps.standard.entity.DapStandardDim;
import cn.com.bluemoon.daps.standard.entity.DapStandardIndex;
import cn.com.bluemoon.daps.standard.service.DapStandardBasicService;
import cn.com.bluemoon.daps.standard.service.DapStandardDimService;
import cn.com.bluemoon.daps.standard.service.DapStandardIndexService;
import cn.com.bluemoon.daps.standard.service.TopicCategoryDeletePredicate;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 标准中与主题分类相关的约束逻辑
 *
 * @author Jarod.Kong
 */
@Service
@Slf4j
public class StandardModuleTopicCategoryDeletePredicate implements TopicCategoryDeletePredicate {
    @Lazy
    @Autowired
    private DapStandardIndexService indexService;
    @Lazy
    @Autowired
    private DapStandardBasicService basicService;

    @Lazy
    @Autowired
    private DapStandardDimService dimService;

    @Override
    public Result<String> predicate(String topicCategoryId) {
        // 指标标准
        LambdaQueryWrapper<DapStandardIndex> eq = indexService.bmLambdaQuery().and(wrapper -> {
            return wrapper.eq(DapStandardIndex::getTheme, topicCategoryId)
                    .or()
                    .eq(DapStandardIndex::getPrimarySort, topicCategoryId)
                    .or()
                    .eq(DapStandardIndex::getSecondarySort, topicCategoryId)
                    .or()
                    .eq(DapStandardIndex::getThirdSort, topicCategoryId);
        });
        int cnt = indexService.count(eq);
        if (cnt > 0) {
            return Result.<String>builder().canDelete(false).message("指标标准已引用该主题分类").build();
        }
        // 基础标准
        LambdaQueryWrapper<DapStandardBasic> eqBasic = basicService.bmLambdaQuery().and(wrapper -> {
            return wrapper.eq(DapStandardBasic::getTopicId, topicCategoryId)
                    .or()
                    .eq(DapStandardBasic::getFirstCategory, topicCategoryId)
                    .or()
                    .eq(DapStandardBasic::getSecondCategory, topicCategoryId)
                    .or()
                    .eq(DapStandardBasic::getThirdCategory, topicCategoryId);
        });
        int cntBasic = basicService.count(eqBasic);
        if (cntBasic > 0) {
            return Result.<String>builder().canDelete(false).message("基础标准已引用该主题分类").build();
        }
        // 维度管理
        LambdaQueryWrapper<DapStandardDim> topicEq = dimService.bmLambdaQuery().eq(DapStandardDim::getThemeId, topicCategoryId);
        int cntTopic = dimService.count(topicEq);
        if (cntTopic > 0) {
            return Result.<String>builder().canDelete(false).message("维度中已引用该主题").build();
        }
        // 默认可删除
        return Result.<String>builder().canDelete(true).build();
    }
}
